package com.sundablog.service.impl.systemsetup.permissionsManage.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundablog.mapper.AdminPermissionMapper;
import com.sundablog.mapper.AdminRolePermissionMapper;
import com.sundablog.mapper.AdminUserMapper;
import com.sundablog.mapper.AdminUserRoleMapper;
import com.sundablog.pojo.AdminPermission;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.pojo.AdminRolePermissionExample;
import com.sundablog.pojo.AdminUser;
import com.sundablog.pojo.AdminUserExample;
import com.sundablog.pojo.AdminUserExample.Criteria;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.pojo.AdminUserRoleExample;
import com.sundablog.result.LayUiResult;
import com.sundablog.result.MenuResult;
import com.sundablog.service.systemsetup.permissionsManage.user.UserService;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;

/**
 * 后台用户接口实现类
 * 
 * @ClassName: UserServiceImpl
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: 哒哒
 * @date: 2018年1月31日 下午2:11:00
 * 
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private AdminUserMapper adminUserMapper;

	@Autowired
	private AdminUserRoleMapper adminUserRoleMapper;

	@Autowired
	private AdminRolePermissionMapper adminRolePermissionMapper;

	@Autowired
	private AdminPermissionMapper adminPermissionMapper;

	/**
	 * 分页查询 Title: findPagingQuery Description:
	 * 
	 * @param page
	 * @param limit
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.user.UserService#findPagingQuery(int,
	 *      int)
	 */
	@Override
	public LayUiResult findPagingQuery(int page, int limit) {
		PageHelper.startPage(page, limit);
		AdminUserExample example = new AdminUserExample();
		List<AdminUser> list = adminUserMapper.selectByExample(example);
		PageInfo<AdminUser> pageInfo = new PageInfo<>(list);
		LayUiResult layUiResult = new LayUiResult();
		layUiResult.setCode(0);
		layUiResult.setMsg("");
		layUiResult.setCount(pageInfo.getTotal());
		layUiResult.setData(list);
		return layUiResult;
	}

	/**
	 * 添加一条管理员用户 Title: addUser Description:
	 * 
	 * @param adminUser
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.user.UserService#addUser(com.sundablog.pojo.AdminUser)
	 */
	@Override
	public int addUser(AdminUser adminUser, int roleId) {
		// 检查手机号
		AdminUserExample example = new AdminUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(adminUser.getPhone());
		List<AdminUser> list = adminUserMapper.selectByExample(example);
		if (list.size() > 0) {
			return 0;
		} else {
			// 检查用户名
			AdminUserExample example1 = new AdminUserExample();
			Criteria criteria2 = example1.createCriteria();
			criteria2.andUsernameEqualTo(adminUser.getUsername());
			List<AdminUser> list2 = adminUserMapper.selectByExample(example1);
			if (list2.size() > 0) {
				return 0;
			} else {
				String salt = RandomUtil.simpleUUID();
				adminUser.setSalt(salt);
				adminUser.setPassword(DigestUtil.md5Hex(adminUser.getPassword() + salt));
				adminUser.setLocked(0);
				adminUser.setCtime(new Date());
				int i = adminUserMapper.insert(adminUser);
				if (i > 0) {
					// 查询管理员信息
					AdminUserExample example2 = new AdminUserExample();
					Criteria criteria3 = example2.createCriteria();
					criteria3.andPhoneEqualTo(adminUser.getPhone());
					criteria3.andUsernameEqualTo(adminUser.getUsername());
					List<AdminUser> list3 = adminUserMapper.selectByExample(example1);
					AdminUser user = list3.get(0);
					// 添加角色权限关联
					AdminUserRole adminUserRole = new AdminUserRole();
					adminUserRole.setRoleId(roleId);
					adminUserRole.setUserId(user.getUserId());
					int j = adminUserRoleMapper.insert(adminUserRole);
					return j;
				} else {
					return 0;
				}
			}
		}
	}

	/**
	 * 根据id查询一条管理员 Title: selectAdminUserById Description:
	 * 
	 * @param id
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.user.UserService#selectAdminUserById(int)
	 */
	@Override
	public AdminUser selectAdminUserById(int id) {
		AdminUser adminUser = adminUserMapper.selectByPrimaryKey(id);
		adminUser.setPassword("");
		adminUser.setSalt("");
		return adminUser;
	}

	/**
	 * 修改一条数据 Title: updataAdminUser Description:
	 * 
	 * @param adminUser
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.user.UserService#updataAdminUser(com.sundablog.pojo.AdminUser)
	 */
	@Override
	public int updataAdminUser(AdminUser adminUser) {
		AdminUser user = adminUserMapper.selectByPrimaryKey(adminUser.getUserId());
		// 判断用户名是否修改
		if (user.getUsername().equals(adminUser.getUsername())) {
			String salt = RandomUtil.simpleUUID();
			adminUser.setSalt(salt);
			adminUser.setPassword(DigestUtil.md5Hex(adminUser.getPassword() + salt));
			adminUser.setLocked(0);
			adminUser.setCtime(new Date());
			int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
			return i;
		}
		// 判断手机号是否修改
		if (user.getPhone().equals(adminUser.getPhone())) {
			String salt = RandomUtil.simpleUUID();
			adminUser.setSalt(salt);
			adminUser.setPassword(DigestUtil.md5Hex(adminUser.getPassword() + salt));
			adminUser.setCtime(new Date());
			int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
			return i;
		}

		// 检查手机号
		AdminUserExample example = new AdminUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(adminUser.getPhone());
		List<AdminUser> list = adminUserMapper.selectByExample(example);
		if (list.size() > 0) {
			return 0;
		} else {
			// 检查用户名
			AdminUserExample example1 = new AdminUserExample();
			Criteria criteria2 = example1.createCriteria();
			criteria2.andUsernameEqualTo(adminUser.getUsername());
			List<AdminUser> list2 = adminUserMapper.selectByExample(example1);
			if (list2.size() > 0) {
				return 0;
			} else {
				String salt = RandomUtil.simpleUUID();
				adminUser.setSalt(salt);
				adminUser.setPassword(DigestUtil.md5Hex(adminUser.getPassword() + salt));
				adminUser.setLocked(0);
				adminUser.setCtime(new Date());
				int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
				return i;
			}
		}

	}

	/**
	 * 根据ID禁用一条数据 Title: updataDisableAdminUser Description:
	 * 
	 * @param id
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.user.UserService#updataDisableAdminUser(int)
	 */
	@Override
	public int updataDisableAdminUser(int id) {
		AdminUser adminUser = adminUserMapper.selectByPrimaryKey(id);
		if (adminUser.getLocked() == 1) {
			adminUser.setLocked(0);
		} else {
			adminUser.setLocked(1);
		}
		int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
		return i;
	}

	/**
	 * 删除一条用户信息 Title: delectAdminUser Description:
	 * 
	 * @param id
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.user.UserService#delectAdminUser(int)
	 */
	@Override
	public int delectAdminUser(int id) {
		int i = adminUserMapper.deleteByPrimaryKey(id);
		if (i > 0) {
			// 删除角色权限关联
			AdminUserRoleExample example = new AdminUserRoleExample();
			com.sundablog.pojo.AdminUserRoleExample.Criteria criteria = example.createCriteria();
			criteria.andUserIdEqualTo(id);
			int j = adminUserRoleMapper.deleteByExample(example);
			return j;
		} else {
			return 0;
		}
	}

	/**
	 * 修改管理员性别 Title: updataAdminUserSex Description:
	 * 
	 * @param id
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.user.UserService#updataAdminUserSex(int)
	 */
	@Override
	public int updataAdminUserSex(int id) {
		AdminUser adminUser = adminUserMapper.selectByPrimaryKey(id);
		if (adminUser.getSex() == 1) {
			adminUser.setSex(0);
		} else {
			adminUser.setSex(1);
		}
		int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
		return i;
	}

	/**
	 * 根据用户名获取用户信息
	 * Title: selectByUserName   
	 * Description:    
	 * @param userName
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.user.UserService#selectByUserName(java.lang.String)
	 */
	@Override
	public AdminUser selectByUserName(String userName) {
		AdminUserExample example = new AdminUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(userName);
		List<AdminUser> list = adminUserMapper.selectByExample(example);
		AdminUser adminUser = list.get(0);
		return adminUser;
	}

	
	
	


}
